package controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pojo.RegistrationRecords;
import service.RegistrationRecordsService;

import java.util.List;
import java.util.Map;

@Api(tags = "挂号记录管理")
@RestController
@CrossOrigin(origins = "http://localhost:5173")
@RequestMapping("/registration")
public class RegistrationRecordsController {
    @Autowired
    private RegistrationRecordsService registrationRecordsService;

    @ApiOperation(value = "获取所有挂号记录", notes = "查询系统中所有的挂号记录信息")
    @GetMapping("/all")
    public List<RegistrationRecords> getAllRecords() {
        return registrationRecordsService.selectAllRecords();
    }

    @ApiOperation(value = "根据医生ID查询挂号记录", notes = "通过医生ID获取该医生的所有挂号记录")
    @ApiImplicitParam(name = "docId", value = "医生ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/doctor/{docId}")
    public List<RegistrationRecords> getByDoctorId(@PathVariable String docId) {
        return registrationRecordsService.selectByDoctorId(docId);
    }

    // 新增：按医生ID和日期数组查询
    @ApiOperation(value = "根据医生ID和日期范围查询挂号记录", notes = "通过医生ID和日期数组获取挂号记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "docId", value = "医生ID", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "days", value = "日期数组", required = true, dataType = "String", allowMultiple = true, paramType = "query")
    })
    @GetMapping("/doctor/{docId}/by-days")
    public List<RegistrationRecords> getByDoctorIdAndDays(
            @PathVariable String docId,
            @RequestParam(value = "days") List<String> days) {
        return registrationRecordsService.selectByDoctorIdAndDays(docId, days);
    }





    @ApiOperation(value = "根据日期查询挂号记录", notes = "查询指定日期的所有挂号记录")
    @ApiImplicitParam(name = "day", value = "日期（格式：YYYY-MM-DD）", required = true, dataType = "String", paramType = "path")
    @GetMapping("/date/{day}")
    public List<RegistrationRecords> getByDate(@PathVariable String day) {
        return registrationRecordsService.selectByDate(day);
    }

    @ApiOperation(value = "统计各科室挂号数量", notes = "统计每个科室的挂号记录总数，并按数量降序排列")
    @GetMapping("/department/count")
    public List<Map<String, Object>> getCountByDepartment() {
        return registrationRecordsService.countByDepartment();
    }

    @ApiOperation(value = "获取医生排班信息", notes = "查询未来日期的医生排班及号源信息")
    @GetMapping("/schedules")
    public List<RegistrationRecords> getDoctorSchedules() {
        return registrationRecordsService.selectDoctorSchedules();
    }

    @ApiOperation(value = "新增挂号记录", notes = "创建一条新的挂号记录")
    @ApiImplicitParam(name = "record", value = "挂号记录实体", required = true, dataType = "RegistrationRecords", paramType = "body")
    @PostMapping("/add")
    public int addRecord(@RequestBody RegistrationRecords record) {
        return registrationRecordsService.insert(record);
    }

    @ApiOperation(value = "更新挂号记录", notes = "根据ID更新挂号记录信息")
    @ApiImplicitParam(name = "record", value = "挂号记录实体，需包含ID", required = true, dataType = "RegistrationRecords", paramType = "body")
    @PutMapping("/update")
    public int updateRecord(@RequestBody RegistrationRecords record) {
        return registrationRecordsService.updateByPrimaryKey(record);
    }

    @ApiOperation(value = "根据患者ID查询挂号记录", notes = "查询指定患者的所有挂号记录")
    @ApiImplicitParam(name = "patientId", value = "患者ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/patient/{patientId}")
    public List<RegistrationRecords> getByPatientId(@PathVariable String patientId) {
        return registrationRecordsService.selectByPatientId(patientId);
    }

    @ApiOperation(value = "删除挂号记录", notes = "根据ID删除指定的挂号记录")
    @ApiImplicitParam(name = "id", value = "挂号记录ID", required = true, dataType = "Integer", paramType = "path")
    @DeleteMapping("/delete/{id}")
    public int deleteRecord(@PathVariable Integer id) {
        return registrationRecordsService.deleteByPrimaryKey(id);
    }

    @ApiOperation(value = "根据ID查询挂号记录", notes = "查询指定ID的挂号记录详情")
    @ApiImplicitParam(name = "id", value = "挂号记录ID", required = true, dataType = "Integer", paramType = "path")
    @GetMapping("/{id}")
    public RegistrationRecords getRecordById(@PathVariable Integer id) {
        return registrationRecordsService.selectByPrimaryKey(id);
    }
}